package com.joinway.admin.service;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * each time open the connection, a new server instance will be created, hence thread safe
 * @author 
 *
 */
@Deprecated
@ServerEndpoint(value = "/ws/term")
public class TermService {

	private final static Logger log = LoggerFactory.getLogger(TermService.class);

	private final static String HOST_KEY = "host";
	private final static String NAME_KEY = "name";
	private final static String PASSWORD_KEY = "password";

//	SshClientAdaptor client;

//	String currentCommand = null;
	
	boolean effectOutput = true;
	
	@OnOpen
	public void open(Session session) {
//		try {
//			if (client != null) {
//				log.debug("close previous ssh connection ...");
//				client.logout();
//			}
//
//			List<NameValuePair> nvps = URLEncodedUtils.parse(session.getQueryString(), Consts.UTF_8);
//
//			if (CollectionUtils.isNotEmpty(nvps)) {
//				String host = "";
//				String user = "";
//				String password = "";
//
//				for (NameValuePair nvp : nvps) {
//					String name = nvp.getName();
//					String value = nvp.getValue();
//
//					if (NAME_KEY.equals(name)) {
//						user = value;
//					} else if (PASSWORD_KEY.equals(name)) {
//						password = value;
//					} else if (HOST_KEY.equals(name)) {
//						host = value;
//					}
//				}
//
//				log.debug("connect to host {} ...", host);
//				client = new SshClientAdaptor(host, user, password, new SshOutputCallback(){
//					@Override
//					public void process(String line) throws IOException {
//						System.out.print(line);
//						if(effectOutput){
//							session.getBasicRemote().sendText(line);
//						}else{
//							effectOutput = true;
//						}
//					}
//				});
//
//				client.login();
//			}
//		} catch (Exception e) {
//			log.error("create ssh session failed!", e);
//		}
//		
//		log.info("client {} was connected", session.getId());
	}

	@OnMessage
	public void incoming(String message) {
//		try {
//			log.debug("input command is [{}]", message);
//			effectOutput = false;
//			client.execute(message);
//		} catch (IOException e) {
//			log.error("failed to execute command [" + message + "]", e);
//		}
	}
	
	@OnClose
	public void close(Session session) {
//		client.logout();
		log.info("client {} was closed", session.getId());
	}
}
